package com.ming_za.od_updater_sdk.transmit;

import com.ming_za.od_updater_sdk.IUpdater;
import com.ming_za.od_updater_sdk.obj.Res;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 基础流传输实现
 * <p>如果您需要实现进度监听等功能，可继承该类，重写{@link #postTransmit(long, long, Res,IUpdater)}方法</p>
 */
public class BasisStreamTransmit implements IStreamTransmit {

    @Override
    public void transmit(OutputStream outputStream, byte[] buff, int buffSize, IUpdater odUpdater) throws IOException {
        outputStream.write(buff,0,buffSize);
    }

    private long lastShow = 0;

    @Override
    public void postTransmit(long transmissionTotalSize, long fileSize, Res res, IUpdater odUpdater) {
        if (System.currentTimeMillis() - lastShow > 1000) {
            lastShow = System.currentTimeMillis();
            odUpdater.getLogger().debug("[" + res.getId() + "]下载进度-->" +
                    new BigDecimal(transmissionTotalSize / (fileSize + 0D) * 100).setScale(2, RoundingMode.HALF_UP) + "% | "
                    + transmissionTotalSize + "/" + fileSize + "字节");
        }
    }
}
